package com.link.base.wechat.common.service;

import com.alibaba.fastjson.JSONObject;
import com.link.base.wechat.common.util.WxQRCodeUtil;
import com.link.base.wechat.wxtoken.model.WxToken;
import com.link.base.wechat.wxtoken.service.WxTokenService;
import com.link.core.basic.service.ServiceException;
import com.link.core.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 基本的微信工具服务
 * 
 * @author ouyangzhenqiang
 * @date 2019/03/14 20:56
 */
@Service
public class WxCommonServiceImpl implements WxCommonService {
    @Resource
    private WxTokenService wxTokenService;

    @Override
    public String createTempQrCode(String wxAppId, String scene_id, String time) throws Exception {
        if (StringUtils.isBlank(wxAppId)) {
            throw new ServiceException("WXCOMMON-001");
        }
        WxToken wxToken = wxTokenService.getWxAccessTokenByAppId(wxAppId);
        JSONObject json = WxQRCodeUtil.createTempQrCode(wxToken.getAccess_token(), scene_id, time);
        String ticket = json.getString("ticket");
        return WxQRCodeUtil.getWxQrCodeUrlByTicket(ticket);
    }

    @Override
    public String createForeverQrCode(String wxAppId, String scene_str) throws Exception {
        if (StringUtils.isBlank(wxAppId)) {
            throw new ServiceException("WXCOMMON-001");
        }
        WxToken wxToken = wxTokenService.getWxAccessTokenByAppId(wxAppId);
        JSONObject json = WxQRCodeUtil.createForeverQrCode(wxToken.getAccess_token(), scene_str);
        String ticket = json.getString("ticket");
        return WxQRCodeUtil.getWxQrCodeUrlByTicket(ticket);
    }

    @Override
    public String getWxQrCodeUrlByTicket(String ticket) throws Exception {
        return WxQRCodeUtil.getWxQrCodeUrlByTicket(ticket);
    }
}
